using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DTO;
using BLL;
using System.IO;
using System.Linq;

namespace WellmeadowsHospital
{
    public partial class RibbonFormHospital : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        StaffBLL staffBLL = new StaffBLL();
        QualificationBLL qualificationBLL = new QualificationBLL();
        ExperienceBLL experienceBLL = new ExperienceBLL();
        SProcBLL SPBLL = new SProcBLL();
        JobBLL jobBLL = new JobBLL();
        PositionBLL positionBLL = new PositionBLL();
        StaffOfWardBLL sowBLL = new StaffOfWardBLL();
        WardBLL wardBLL = new WardBLL();
        UserBLL userBLL = new UserBLL();
        private string position;

        PatientBLL patientBLL = new PatientBLL();
        NextOfKinBLL nokBLL = new NextOfKinBLL();
        LocalDoctorBLL localdoctorBLL = new LocalDoctorBLL();
        AppointmentBLL appointmentBLL = new AppointmentBLL();
        //khai bao cac bien de edit trong tab add patient
        string[] pa_nok_ld_ID = new string[3];
        //bien de edit Appointment trong Add Appointment
        string appointmentid;
        public RibbonFormHospital()
        {
            InitializeComponent();
        }

        private void ribbon_Click(object sender, EventArgs e)
        {

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panelControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panelControl3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void groupControl4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textEdit19_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void pcRelocate2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textEdit29_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void xtcHome_CloseButtonClick(object sender, EventArgs e)
        {
            xtcHome.TabPages.RemoveAt(xtcHome.SelectedTabPageIndex);
        }

        private void textEdit43_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void pcAddPatients2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panelControl10_Paint(object sender, PaintEventArgs e)
        {

        }

        private void labelControl112_Click(object sender, EventArgs e)
        {

        }

        private void gridControl13_Click(object sender, EventArgs e)
        {

        }

        private void labelControl119_Click(object sender, EventArgs e)
        {

        }

        private void groupControl26_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textEdit128_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void pcAddSupplier5_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gridControl47_Click(object sender, EventArgs e)
        {

        }
        //tao moi mot nhan vien moi
        private void btnAddPersional1_Click(object sender, EventArgs e)
        {
            StaffDTO staff = new StaffDTO();
            staff.FirstName = txtFirstName1.Text;
            staff.Lastname = txtLastName1.Text;
            staff.Address = txtAddress1.Text;
            staff.Sex = cbbSex1.SelectedItem.ToString();
            staff.Dob = deDOB1.DateTime;
            staff.Tel = txtTel1.Text;
            staff.Nin = txtNIN1.Text;
            staff.PictureProfile = peImage1.Image;

            staff.Status = "New Staff";


            if (staffBLL.Insert(staff).Equals("Thanh cong"))
            {
                MessageBox.Show("Insert successful");
                gcStaffList1.DataSource = staffBLL.SelectDesc();
                lblStaffID1.Text = gridView6.GetRowCellValue(gridView6.SelectedRowsCount - 1, StaffID).ToString();
            }
            else
            {
                MessageBox.Show(staffBLL.Insert(staff));
            }

        }

        private void deStartDate_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cbbSex1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void xtraTabPage3_Paint(object sender, PaintEventArgs e)
        {

        }

        //khi mot tab duoc add vao thi no se focus vao tab do
        private void xtcHome_ControlAdded(object sender, ControlEventArgs e)
        {
            xtcHome.SelectedTabPageIndex = xtcHome.TabPages.Count - 1;
        }

        //load tu csdl bang staffs
        private void gcStaffList1_Load(object sender, EventArgs e)
        {
            gcStaffList1.DataSource = staffBLL.SelectDesc();
            //MessageBox.Show((gridView6.SelectedRowsCount-1).ToString());

            //hien thi StaffID cau dong dau tien trong gridview 
            lblStaffID1.Text = gridView6.GetRowCellValue(gridView6.SelectedRowsCount - 1, StaffID).ToString();
        }

        private void RibbonFormHospital_Load(object sender, EventArgs e)
        {
            //tat bo tat ca  cac tab
            while (xtcHome.TabPages.Count != 0)
            {
                for (int i = 0; i < xtcHome.TabPages.Count; i++)
                {
                    xtcHome.TabPages.RemoveAt(i);
                }
            }
            //mac dinh la tab Home
            xtcHome.TabPages.Add(xtpHome);

        }

        //khi click vao nut add tren ribon thi hien thi tab dieu khien xtpAddSatff
        private void bbtnAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpAddStaff);
        }

        public void ResetStaffInfo()
        {
            txtFirstName1.Text = "";
            txtLastName1.Text = "";
            txtAddress1.Text = "";
            cbbSex1.EditValue = "";
            cbbSex1.EditValue = "Male";
            deDOB1.EditValue = "";
            txtTel1.Text = "";
            txtNIN1.Text = "";
            peImage1.EditValue = "";
            btnAddPersional1.Enabled = true;

        }
        //khi click vao cac row thi thay doi gia tri text cua StaffID
        private void gridView6_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            lblStaffID1.Text = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcStaffID1).ToString();

            //edit thong tin staff
            ResetStaffInfo();
            string[] name = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcName1).ToString().Split(' ');
            txtFirstName1.Text = name[0];
            txtLastName1.Text = name[1];
            txtAddress1.Text = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcAddress1).ToString();
            cbbSex1.SelectedItem = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcSex1).ToString();
            deDOB1.DateTime = Convert.ToDateTime(gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcDateOfBirth1));
            txtTel1.Text = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcTel1).ToString();
            txtNIN1.Text = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcNIN1).ToString();

            //an hien cac nut add va edit
            btnAddPersional1.Enabled = false;
            //hien thi nut add cau qualification  va  experience 
            btnAddQualification.Enabled = true;
            btnAddExperience1.Enabled = true;
            //reset cac field cua qualification de add moi
            txtType1.Text = "";
            deDate1.EditValue = null;
            txtInstitution1.Text = "";

            //Load anh vao pictureedit

            if (gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcImageProfile1) != null)
            {
                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcImageProfile1).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peImage1.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peImage1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peImage1.EditValue = "";
            }

        }

		 private void addPatientH_Click(object sender, EventArgs e)
        {
            NextOfKinDTO nhannok = new NextOfKinDTO();
            nhannok.Fullname = nextkinFullNameH.Text;
            nhannok.Relationship = nextkinRelationH.Text;
            nhannok.Address = nextkinAddressH.Text;
            nhannok.Tel = nextkinTelH.Text;
            LocalDoctorDTO nhanld = new LocalDoctorDTO();
            nhanld.Fullname = localdoctorFulNameH.Text;
            nhanld.Clinicno = localdoctorClinicNoH.Text;
            nhanld.Address = localdoctorAddressH.Text;
            nhanld.Tel = localdoctorTelH.Text;

            PatientDTO nhanpa = new PatientDTO();
            nhanpa.FirstName = paFirstNameH.Text;
            nhanpa.Lastname = paLastNameH.Text;
            nhanpa.Address = paAddressH.Text;
            nhanpa.Sex = paSexH.SelectedItem.ToString();
            nhanpa.Tel = paTelH.Text;
            nhanpa.Dob = paDobH.DateTime;
            nhanpa.Maritalstatus = paMariH.SelectedItem.ToString();
            nhanpa.Dateregistered = paDateRegisH.DateTime;
            nhanpa.Status = paStatusH.Text;
            string kt1 = nextkinFullNameH.Text;
            string kt2 = localdoctorFulNameH.Text;
            if (!kt1.Equals(""))//-----co nextofkin-----------------
            {
                if (!kt2.Equals(""))//--------------co localdoctor------------------
                {
                    if (nokBLL.Insert(nhannok).Equals("success") && localdoctorBLL.Insert(nhanld).Equals("success"))
                    {
                        nhanpa.Localdoctorid = localdoctorBLL.GetID(nhanld);
                        nhanpa.Nokid = nokBLL.GetID(nhannok);
                        if (patientBLL.Insert(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Insert patient, nextofkin and localdoctor successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Insert(nhanpa));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Insert into LocalDoctor, NextOfKin error");
                    }
                }
                else//-------co nextokkin, khong co localdoctor-----------
                {
                    if (nokBLL.Insert(nhannok).Equals("success"))
                    {
                        nhanpa.Nokid = nokBLL.GetID(nhannok);
                        if (patientBLL.Insert(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Insert patient and nextofkin successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Insert(nhanpa));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Insert into NextOfKin error");
                    }
                }
            }
            else//------------khong co nextofkin
            {
                if (!kt2.Equals(""))//------co localdoctor------
                {
                    if (localdoctorBLL.Insert(nhanld).Equals("success"))
                    {
                        nhanpa.Localdoctorid = localdoctorBLL.GetID(nhanld);
                        if (patientBLL.Insert(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Insert patient and localdoctor successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Insert(nhanpa));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Insert into LocalDoctor error");
                    }
                }
                else {//-----------ko co nextofkin va localdoctoc
                    if (patientBLL.Insert(nhanpa).Equals("success"))
                    {
                        MessageBox.Show("Insert patient successful");
                        gcPatients.DataSource = patientBLL.SelectDesc();
                    }
                    else
                    {
                        MessageBox.Show(patientBLL.Insert(nhanpa));
                    }
                }
            }    
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpAddPatients);
            gcPatients.DataSource = patientBLL.SelectDesc();
            buttonEditPatientH.Visible = false;
        }





        private void gridView6_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            //gridView6.GetSelectedRows()[0] lay dong dau tien trong tap row minh dang select
            lblStaffID1.Text = gridView6.GetRowCellValue(gridView6.GetSelectedRows()[0], gcStaffID1).ToString();

        }

        //tao moi mot qualification tuong ung voi StaffID
        private void btnAddQualification_Click(object sender, EventArgs e)
        {
            QualificationDTO qua = new QualificationDTO();
            qua.Staffid = lblStaffID1.Text;
            qua.Type = txtType1.Text;
            qua.Date = deDate1.DateTime;
            qua.Institution = txtInstitution1.Text;
            if (qualificationBLL.Insert(qua).Equals("success"))
            {
                MessageBox.Show("Insert Success");
                gcQualification1.DataSource = qualificationBLL.Select(lblStaffID1.Text);
            }
            else
            {
                MessageBox.Show(qualificationBLL.Insert(qua));
            }
        }

        //khi StaffID thay doi thi no tu dong load lai cac qualification trong gcQualification1 tuong ung voi StaffID do
        private void lblStaffID1_TextChanged(object sender, EventArgs e)
        {
            gcQualification1.DataSource = qualificationBLL.Select(lblStaffID1.Text);
            gcExperience1.DataSource = experienceBLL.Select(lblStaffID1.Text);
            //reset cac field  cua qualification de add moi
            txtType1.Text = "";
            deDate1.EditValue = null;
            txtInstitution1.Text = "";

            //hien thi nut add cau qualification
            btnAddQualification.Enabled = true;

            //reset cac field  cua experience de add moi
            txtPosition1.Text = "";
            deStartDate1.EditValue = null;
            deFinishDate1.EditValue = null;
            txtOrganization1.Text = "";

            //hien nut add cua experience
            btnAddExperience1.Enabled = true;

        }

        //khi click vao cac row thi set cac gia tri len cac component de edit
        private void gvQualificationT_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtType1.Text = gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], Type).ToString();
            deDate1.DateTime = Convert.ToDateTime(gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], Date));
            txtInstitution1.Text = gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], Institution).ToString();

            //an nut add
            btnAddQualification.Enabled = false;
        }

        //reset 
        private void btnResetAddStaff_Click(object sender, EventArgs e)
        {
            txtFirstName1.Text = "";
            txtLastName1.Text = "";
            txtAddress1.Text = "";
            cbbSex1.SelectedText = "Male";
            deDOB1.EditValue = null;
            txtTel1.Text = "";
            txtNIN1.Text = "";
        }

        //update qualification of staff
        private void btnEditAddStaffT_Click(object sender, EventArgs e)
        {

            QualificationDTO qua = new QualificationDTO();
            qua.QualificationID = gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], QualificationID).ToString();
            qua.Type = txtType1.Text;
            qua.Date = deDate1.DateTime;
            qua.Institution = txtInstitution1.Text;
            if (qualificationBLL.Update(qua).Equals("success"))
            {
                MessageBox.Show("Update Successful");
                gcQualification1.DataSource = qualificationBLL.Select(lblStaffID1.Text);
            }
            else
            {
                MessageBox.Show(qualificationBLL.Update(qua));
            }
        }

        //tao moi mot experience tuong ung voi SatffID
        private void btnAddExperience1_Click(object sender, EventArgs e)
        {
            ExperienceDTO exp = new ExperienceDTO();
            exp.StaffID = lblStaffID1.Text;
            exp.Position = txtPosition1.Text;
            exp.StartDate = deStartDate1.DateTime;
            exp.FinishDate = deFinishDate1.DateTime;
            exp.Organization = txtOrganization1.Text;
            if (experienceBLL.Insert(exp).Equals("success"))
            {
                MessageBox.Show("Insert successful");
                gcExperience1.DataSource = experienceBLL.Select(lblStaffID1.Text);
            }
            else
            {
                MessageBox.Show(experienceBLL.Insert(exp));
            }

        }

        private void gvExperienceAddStaff_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtPosition1.Text = gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], PositionExp).ToString();
            deStartDate1.DateTime = Convert.ToDateTime(gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], StartDateExp));
            deFinishDate1.DateTime = Convert.ToDateTime(gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], FinishDateExp));
            txtOrganization1.Text = gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], OrganizationExp).ToString();

            //an nut add 
            btnAddExperience1.Enabled = false;
        }

        private void btnEditAddStaff_Click(object sender, EventArgs e)
        {
            ExperienceDTO exp = new ExperienceDTO();
            exp.ExperienceId = gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], IDExp).ToString();
            exp.Position = txtPosition1.Text;
            exp.StartDate = deStartDate1.DateTime;
            exp.FinishDate = deFinishDate1.DateTime;
            exp.Organization = txtOrganization1.Text;

            if (experienceBLL.Update(exp).Equals("success"))
            {
                MessageBox.Show("Update successful");
                gcExperience1.DataSource = experienceBLL.Select(lblStaffID1.Text);
            }
            else
            {
                MessageBox.Show(experienceBLL.Update(exp));
            }
        }

        //cai dat su kien cho nut search trong add Staff

        private void txtSearchAddStaff_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcStaffList1.DataSource = staffBLL.SearchIDName(txtSearchAddStaff.Text);

        }

        //cai dat phan Allocate Staff
        private void bbtnAllocated_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpAllocated);
        }

        //load cac staff len gridcontrol
        private void gcStaffListAllocated_Load(object sender, EventArgs e)
        {
            gcStaffListAllocated.DataSource = staffBLL.SelectNewStaff();
            //lay ve mot PositionDTO chua cac position in Hospital
            foreach (PositionDTO item in positionBLL.SelectAll())
            {
                cbbPostitionAllocate.Properties.Items.Add(item.Position);
            }
        }

        //lay gia tri id cua Staff trong danh sach de cap phat job cho cac ward
        private void gvNewStaffAllocated_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtAllocateAllocatedT.Enabled = true;
            txtStaffIDAllocateT.Text = gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], StaffID).ToString();
            if (gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStatus).ToString().Equals("Work"))
            {
                List<JobDTO> list = staffBLL.ReceiveJob(gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], StaffID).ToString());
                //lay WardID tuong ung voi job cua nhan vien
                List<string> li = staffBLL.ReceiveWard(gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], StaffID).ToString());
                string item = "";
                foreach (string a in li)
                {
                    item = a;
                }
                foreach (JobDTO job in list)
                {
                    txtAllocateAllocatedT.Text = item;
                    gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
                    cbbPostitionAllocate.SelectedItem = job.PositionName;
                    position = job.PositionName;
                    txtHoursT.Text = job.Hours.ToString();
                    txtCurrentSalaryT.Text = job.CurrentSalary.ToString();
                    txtSalaryScaleT.Text = job.SalaryScale;
                    cbbPaidT.SelectedItem = job.Paid;
                    cbbPTT.SelectedItem = job.Contract;
                    deStartDateT.DateTime = job.StartDate;
                    deFinishDateAllocate.Enabled = true;
                }
            }


        }
        public void ResetAllocated()
        {
            txtStaffIDAllocateT.Text = "";
            txtAllocateAllocatedT.Text = "";
            cbbPostitionAllocate.EditValue = "";
            cbbPostitionAllocate.EditValue = "Select Position";
            txtHoursT.Text = "";
            txtCurrentSalaryT.Text = "";
            txtSalaryScaleT.Text = "";
            cbbPaidT.EditValue = "W";
            cbbPTT.EditValue = "P";
            deStartDateT.EditValue = "";
        }

        //load danh sach cac ward khi load
        private void gcWardListAllocated_Load(object sender, EventArgs e)
        {
            gcWardListAllocated.DataSource = wardBLL.SelectAllWard();
        }

        //cap phat job cho cac staff in ward
        private void btnSaveAllocateT_Click(object sender, EventArgs e)
        {
            if (SPBLL.KiemtraPositionSample(cbbPostitionAllocate.SelectedItem.ToString(), txtStaffIDAllocateT.Text, txtAllocateAllocatedT.Text))
            {
                MessageBox.Show("Same position of staff work in ward");
            }
            else
            {
                if (cbbPostitionAllocate.SelectedIndex != -1)
                {


                    if (cbbPostitionAllocate.SelectedItem.Equals(position))
                    {
                        if (gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStatus).ToString().Equals("Work"))
                        {
                            if (!txtAllocateAllocatedT.Text.Equals(""))
                            {
                                StaffOfWardDTO sw = new StaffOfWardDTO();
                                sw.StaffID = txtStaffIDAllocateT.Text;
                                sw.WardId = txtAllocateAllocatedT.Text;
                                sw.StartDate = deStartDateT.DateTime;
                                if (sowBLL.Insert(sw).Equals("success"))
                                {
                                    MessageBox.Show("Insert Successful");
                                }
                                else
                                {
                                    MessageBox.Show(sowBLL.Insert(sw));
                                }
                            }
                            gcStaffListAllocated.DataSource = staffBLL.SelectNewStaff();
                            gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
                        }
                    }
                    else
                    {
                        JobDTO job = new JobDTO();
                        job.StaffId = txtStaffIDAllocateT.Text;
                        foreach (PositionDTO item in positionBLL.SelectAll())
                        {
                            if (cbbPostitionAllocate.SelectedItem.ToString().Equals(item.Position))
                            {
                                job.Position = item.PositionId;
                            }
                        }
                        job.CurrentSalary = Convert.ToInt32(txtCurrentSalaryT.Text);
                        job.SalaryScale = txtSalaryScaleT.Text;
                        job.Hours = float.Parse(txtHoursT.Text);
                        job.Paid = cbbPaidT.SelectedItem.ToString();
                        job.Contract = cbbPTT.SelectedItem.ToString();
                        job.StartDate = deStartDateT.DateTime;

                        if (jobBLL.Insert(job).Equals("success"))
                        {
                            if (!txtAllocateAllocatedT.Text.Equals(""))
                            {
                                StaffOfWardDTO sw = new StaffOfWardDTO();
                                sw.StaffID = txtStaffIDAllocateT.Text;
                                sw.WardId = txtAllocateAllocatedT.Text;
                                sw.StartDate = deStartDateT.DateTime;
                                if (sowBLL.Insert(sw).Equals("success"))
                                {
                                    MessageBox.Show("Insert Successful");
                                }
                                else
                                {
                                    MessageBox.Show(sowBLL.Insert(sw));
                                }
                            }
                            gcStaffListAllocated.DataSource = staffBLL.SelectNewStaff();
                            gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
                        }
                        else
                        {
                            MessageBox.Show(jobBLL.Insert(job));
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please choose postion");
                }
            }


            textEdit6.Text = "";
        }


        private void gvWardListAllocated_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtAllocateAllocatedT.Text = gvWardListAllocated.GetRowCellValue(gvWardListAllocated.GetSelectedRows()[0], gcWardID).ToString();
            gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
        }

        private void cbbPostitionAllocate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbPostitionAllocate.SelectedItem.Equals("Medical Director") || cbbPostitionAllocate.SelectedItem.Equals("Personnel Officer"))
            {
                txtAllocateAllocatedT.Text = "";
                txtAllocateAllocatedT.Enabled = false;
            }
        }

        private void btnEdit2AddSTaff_Click(object sender, EventArgs e)
        {
            int row = gridView6.SelectedRowsCount;
            StaffDTO staff = new StaffDTO();
            staff.StaffId = lblStaffID1.Text;
            staff.FirstName = txtFirstName1.Text;
            staff.Lastname = txtLastName1.Text;
            staff.Address = txtAddress1.Text;
            staff.Sex = cbbSex1.SelectedItem.ToString();
            staff.Dob = deDOB1.DateTime;
            staff.Tel = txtTel1.Text;
            staff.Nin = txtNIN1.Text;
            staff.PictureProfile = peImage1.Image;


            if (staffBLL.Update(staff).Equals("success"))
            {
                MessageBox.Show("Edit successful");
                gcStaffList1.DataSource = staffBLL.SelectDesc();
                gridView6.SelectRow(row);

            }
            else
            {
                MessageBox.Show(staffBLL.Update(staff));
            }

        }

        private void gcStaffList1_Click(object sender, EventArgs e)
        {
            btnAddPersional1.Enabled = true;
        }
        //cai dat su kien cho nut Search trong Add Patient

        private void txtSearchIDNameT_KeyPress(object sender, KeyPressEventArgs e)
        {
            peImageSearchT.EditValue = "";
            if (rbtnIDName.Checked)
            {
                gcResultSearchStaff.DataSource = staffBLL.SearchIDName(txtSearchIDNameT.Text);
                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff) != null)
                {
                    gcSearchQualification.DataSource = qualificationBLL.Select(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString());
                    gcSearchExperience.DataSource = experienceBLL.Select(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString());
                }
                //Load anh vao pictureedit
                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff) != null)
                {
                    // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                    byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                    MemoryStream ms = new MemoryStream(a);
                    Image image = Image.FromStream(ms);
                    peImageSearchT.EditValue = image;
                    //can bang hinh anh trong pictureEdit 
                    peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
            }
        }
        private void paSearchH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!paSearchH.Equals(""))
            {
                gcPatients.DataSource = patientBLL.Search(paSearchH.Text);
            }
            else
            {
                gcPatients.DataSource = patientBLL.SelectDesc();
            }

        }
        //in Search Patient
        private void txtSearchPaIdNameH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtSearchPaIdNameH.Equals(""))
            {
                gcSearchPatientH.DataSource = patientBLL.Search(txtSearchPaIdNameH.Text);
            }
            else
            {
                gcSearchPatientH.DataSource = patientBLL.SelectDesc();
            }
        }

                
        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpSearchPatients);
            gcSearchPatientH.DataSource = patientBLL.SelectDesc();
        }


        private void txtSearchPaWithNOK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtSearchPaWithNOK.Equals(""))
            {
                gcSearchPatientH.DataSource = patientBLL.SearchWithNOK(txtSearchPaWithNOK.Text);
            }
            else
            {
                gcSearchPatientH.DataSource = patientBLL.SelectDesc();
            }
        }


        private void bbtnAddAppointment_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpAppontment);
            gcPaInAddAppoint.DataSource = patientBLL.SelectNew();
            gcApointListInAddAppoint.DataSource = appointmentBLL.SelectDesc();
            gcStaffInAddAppoint.DataSource = staffBLL.SelectAll();
            ButtonEditAppoitH.Visible = false;
        }

        private void txtSearchPaInAddAppoint_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtSearchPaInAddAppoint.Equals(""))
            {
                gcPaInAddAppoint.DataSource = patientBLL.Search(txtSearchPaInAddAppoint.Text);
            }
            else
            {
                gcPaInAddAppoint.DataSource = patientBLL.SelectDesc();
            }
        }



        private void bbtnSearch_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpSearch);
        }

        private void gcResultSearchStaff_Load(object sender, EventArgs e)
        {
            gcResultSearchStaff.DataSource = staffBLL.SelectAll();
        }


        private void gvSearchStaffT_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //Load anh vao pictureedit

            if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcImageProfileSearchStaff) != null)
            {

                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peImageSearchT.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peImageSearchT.EditValue = "";
            }
        }

        //reset cac text edit
        public void resetSearchStaff()
        {
            txtSearchTypeT.Text = "";
            txtSearchExperienceT.Text = "";
            txtSearchIDNameT.Text = "";
        }


        private void rbtnIDName_Click(object sender, EventArgs e)
        {
            txtSearchTypeT.Enabled = false;
            txtSearchExperienceT.Enabled = false;
            txtSearchIDNameT.Enabled = true;
            resetSearchStaff();
        }

        private void rbtnType_Click(object sender, EventArgs e)
        {
            txtSearchIDNameT.Enabled = false;
            txtSearchExperienceT.Enabled = false;
            txtSearchTypeT.Enabled = true;
            resetSearchStaff();
        }

        private void rbtnExperience_Click(object sender, EventArgs e)
        {
            txtSearchTypeT.Enabled = false;
            txtSearchIDNameT.Enabled = false;
            txtSearchExperienceT.Enabled = true;
            resetSearchStaff();
        }

        private void txtSearchTypeT_KeyPress(object sender, KeyPressEventArgs e)
        {
            peImageSearchT.EditValue = "";
            if (rbtnType.Checked)
            {
                gcResultSearchStaff.DataSource = staffBLL.SearchType(txtSearchTypeT.Text);
                gcSearchQualification.DataSource = qualificationBLL.Select(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString());
                gcSearchExperience.DataSource = experienceBLL.Select(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString());
                //Load anh vao pictureedit

                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff) != null)
                {

                    // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                    byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                    MemoryStream ms = new MemoryStream(a);
                    Image image = Image.FromStream(ms);
                    peImageSearchT.EditValue = image;
                    //can bang hinh anh trong pictureEdit 
                    peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
            }
        }

        private void txtSearchExperienceT_KeyPress(object sender, KeyPressEventArgs e)
        {
            peImageSearchT.EditValue = "";
            if (rbtnExperience.Checked)
            {
                gcResultSearchStaff.DataSource = staffBLL.SearchExperience(txtSearchExperienceT.Text);
                gcSearchQualification.DataSource = qualificationBLL.Select(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString());
                gcSearchExperience.DataSource = experienceBLL.Select(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString());
                //Load anh vao pictureedit

                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff) != null)
                {

                    // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                    byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                    MemoryStream ms = new MemoryStream(a);
                    Image image = Image.FromStream(ms);
                    peImageSearchT.EditValue = image;
                    //can bang hinh anh trong pictureEdit 
                    peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
            }
        }

        //thuoc tab ALLOCATED STAFF
        private void textEdit6_KeyPress(object sender, KeyPressEventArgs e)
        {
            //search trong allocated staff
            gcStaffListAllocated.DataSource = staffBLL.SearchIDName(textEdit6.Text);
        }

        //thuoc tab ALLOCATED STAFF
        private void gvNewStaffAllocated_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            ResetAllocated();
            txtStaffIDAllocateT.Text = gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], StaffID).ToString();
        }

        //thuoc tab ALLOCATED STAFF
        private void btnResetAllocateT_Click(object sender, EventArgs e)
        {
            ResetAllocated();
        }


        private void labelControl48_Click(object sender, EventArgs e)
        {

        }

        private void gcUserStaffList_Load(object sender, EventArgs e)
        {
            gcUserStaffList.DataSource = staffBLL.LaythongtinStaff();
        }

        private void txtUserSearchStaff_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcUserStaffList.DataSource = staffBLL.SearchIDNamePosition(txtUserSearchStaff.Text);
            if (gvUserStaffList.GetRowCellValue(gvUserStaffList.SelectedRowsCount - 1, gcUserStaffID) != null)
            {
                txtUserStaffIDT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.SelectedRowsCount - 1, gcUserStaffID).ToString();
                txtUserNameT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.SelectedRowsCount - 1, gcUserName).ToString();
            }
        }

        private void bbtnAddUsers_ItemClick(object sender, ItemClickEventArgs e)
        {
            xtcHome.TabPages.Add(xtpChuyenCongTac);
            txtUserStaffIDT.Properties.ReadOnly = true;
            txtUserNameT.Properties.ReadOnly = true;
        }

        private void gvUserStaffList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtUserStaffIDT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.GetSelectedRows()[0], gcUserStaffID).ToString();
            txtUserNameT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.GetSelectedRows()[0], gcUserName).ToString();
        }

        private void gvUserList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtUserStaffIDT.Text = gvUserList.GetRowCellValue(gvUserList.GetSelectedRows()[0], gcUserListStaffID).ToString();
            txtUserNameT.Text = gvUserList.GetRowCellValue(gvUserList.GetSelectedRows()[0], gcUserListName).ToString();
        }

        private void btnUserSave_Click(object sender, EventArgs e)
        {
            if (txtUserPasswordT.Text.Equals(txtUserRetypePasswordT.Text))
            {
                UserDTO user = new UserDTO();
                user.StaffID = txtUserStaffIDT.Text;
                user.Password = txtUserPasswordT.Text;
                user.Role = cbbUserRoleT.SelectedItem.ToString();
                user.Status = cbbUserStatusT.SelectedItem.ToString();

                if (userBLL.Insert(user).Equals("success"))
                {
                    MessageBox.Show("Insert success");
                    gcUserList.DataSource = userBLL.Select();
                }
                else
                {
                    MessageBox.Show(userBLL.Insert(user));
                }
            }
            else
            {
                MessageBox.Show("password not match");
                gcUserList.DataSource = userBLL.ToString();
            }
            
        }

        private void gcUserList_Load(object sender, EventArgs e)
        {
            gcUserList.DataSource = userBLL.Select();
        }

        private void paIdInAddAppointH_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void AddAppointAdd_Click(object sender, EventArgs e)
        {
            AppointmentDTO nhanappoint = new AppointmentDTO();
            nhanappoint.Staffid = AddAppointStaffIdH.Text;
            nhanappoint.Patientid = AddAppointPaIdH.Text;
            nhanappoint.Date = AddAppointDateH.DateTime;
            nhanappoint.Time = AddAppointTimeH.Time.ToString();
            nhanappoint.Room = AddAppointRoomH.SelectedItem.ToString();
            nhanappoint.Description = AddAppointDescriptionH.Text;
            nhanappoint.Status = AddAppointStatusH.SelectedItem.ToString();
            if (appointmentBLL.Insert(nhanappoint).Equals("success"))
            {
                MessageBox.Show("Add Apointment success");
                gcApointListInAddAppoint.DataSource = appointmentBLL.SelectDesc();
            }
            else
            {
                MessageBox.Show(appointmentBLL.Insert(nhanappoint));
            }
        }

        private void gridView22_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            AddAppointPaIdH.Text = gridView22.GetRowCellValue(gridView22.GetSelectedRows()[0], gcAddAppointPaID).ToString();
        }

        private void gridView23_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            AddAppointStaffIdH.Text = gridView23.GetRowCellValue(gridView23.GetSelectedRows()[0], gcAddAppointStaffID).ToString();
        }

        private void txtSearchPaWithLocalDoctor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtSearchPaWithLocalDoctor.Equals(""))
            {
                gcSearchPatientH.DataSource = patientBLL.SearchWithLocalDoctor(txtSearchPaWithLocalDoctor.Text);
            }
            else
            {
                gcSearchPatientH.DataSource = patientBLL.SelectDesc();
            }
        }

        private void gridView18_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string key = gridView18.GetRowCellValue(gridView18.GetSelectedRows()[0], gcPatientIdInSearchPaH).ToString();
            gcNOKinSearchPaH.DataSource = nokBLL.SearchWithPatientID(key);
            gcLocalDoctorinSearchPatientH.DataSource = localdoctorBLL.SearchWithPatientID(key);
        }

        private void gridView16_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            buttonEditPatientH.Visible = true;
            string key = gridView16.GetRowCellValue(gridView16.GetSelectedRows()[0], gcPatientID).ToString();
            var pa = patientBLL.Search(key)[0];
            paFirstNameH.Text = pa.FirstName;
            paLastNameH.Text = pa.LastName;
            paAddressH.Text = pa.Address;
            paSexH.Text = pa.Sex;
            paTelH.Text = pa.Tel;
            //paDobH.DateTime = pa.DateOfBirth;
            paMariH.Text = pa.MaritalStatus;
           // paDateRegisH.DateTime = pa.DateRegistered;
            paStatusH.Text = pa.Status;
            pa_nok_ld_ID[0] = pa.PatientID;
            if (pa.NOKID != null)
            {
                var nok = nokBLL.SearchWithPatientID(key)[0];
                pa_nok_ld_ID[1] = nok.NOKID;
                nextkinFullNameH.Text = nok.FullName;
                nextkinRelationH.Text = nok.Relationship;
                nextkinTelH.Text = nok.Tel;
                nextkinAddressH.Text = nok.Address;
            }
            else
            {
                nextkinFullNameH.Text = "";
                nextkinRelationH.Text = "";
                nextkinTelH.Text = "";
                nextkinAddressH.Text = "";
            }
            if (pa.LocalDoctorID!=null)
            {
                var ld = localdoctorBLL.SearchWithPatientID(key)[0];
                pa_nok_ld_ID[2] = ld.LocalDoctorID;               
                localdoctorFulNameH.Text = ld.FullName;
                localdoctorClinicNoH.Text = ld.ClinicNo;
                localdoctorAddressH.Text = ld.Address;
                localdoctorTelH.Text = ld.Tel;
            }
            else
            {
                localdoctorFulNameH.Text = "";
                localdoctorClinicNoH.Text = "";
                localdoctorAddressH.Text = "";
                localdoctorTelH.Text = "";
            }
        }
        //--------------------sua doi thong tin patient----------------
        private void buttonEditPatientH_Click(object sender, EventArgs e)
        {
            NextOfKinDTO nokpaDTO = new NextOfKinDTO();
            nokpaDTO.Nokid=pa_nok_ld_ID[1];
            nokpaDTO.Fullname = nextkinFullNameH.Text;
            nokpaDTO.Relationship = nextkinRelationH.Text;
            nokpaDTO.Address = nextkinAddressH.Text;
            nokpaDTO.Tel = nextkinTelH.Text;

            //LocalDoctorAndPatientDTO ldpaDTO = new LocalDoctorAndPatientDTO();
            LocalDoctorDTO ldpaDTO = new LocalDoctorDTO();
            ldpaDTO.Localdoctorid=pa_nok_ld_ID[2];
            ldpaDTO.Fullname = localdoctorFulNameH.Text;
            ldpaDTO.Clinicno = localdoctorClinicNoH.Text;
            ldpaDTO.Address = localdoctorAddressH.Text;
            ldpaDTO.Tel = localdoctorTelH.Text;

            PatientDTO nhanpa = new PatientDTO();
            nhanpa.Patientid=pa_nok_ld_ID[0];
            nhanpa.FirstName = paFirstNameH.Text;
            nhanpa.Lastname = paLastNameH.Text;
            nhanpa.Address = paAddressH.Text;
            nhanpa.Sex = paSexH.SelectedItem.ToString();
            nhanpa.Tel = paTelH.Text;
            nhanpa.Dob = paDobH.DateTime;
            nhanpa.Maritalstatus = paMariH.SelectedItem.ToString();
            nhanpa.Dateregistered = paDateRegisH.DateTime;
            nhanpa.Status = paStatusH.Text;
            string kt1 = nextkinFullNameH.Text;
            string kt2 = localdoctorFulNameH.Text;

            if (!kt1.Equals(""))//-----co nextofkin-----------------
            {
                if (!kt2.Equals(""))//--------------co localdoctor------------------
                {
                    if (nokBLL.Update(nokpaDTO).Equals("success") && localdoctorBLL.Update(ldpaDTO).Equals("success"))
                    {
                        nhanpa.Localdoctorid =ldpaDTO.Localdoctorid ;
                        nhanpa.Nokid = nokpaDTO.Nokid;
                        if (patientBLL.Update(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Update patient, nextofkin and localdoctor successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Update(nhanpa));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Update into LocalDoctor, NextOfKin error");
                    }
                }
                else//-------co nextokkin, khong co localdoctor-----------
                {
                    if (nokBLL.Update(nokpaDTO).Equals("success"))
                    {
                        nhanpa.Nokid = nokpaDTO.Nokid;
                        if (patientBLL.Update(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Update patient and nextofkin successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Update(nhanpa));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Update into NextOfKin error");
                    }
                }
            }
            else//------------khong co nextofkin
            {
                if (!kt2.Equals(""))//------co localdoctor------
                {
                    if (localdoctorBLL.Update(ldpaDTO).Equals("success"))
                    {
                        nhanpa.Localdoctorid = ldpaDTO.Localdoctorid;
                        if (patientBLL.Update(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Update patient and localdoctor successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Update(nhanpa));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Update into LocalDoctor error");
                    }
                }
                else
                {//-----------ko co nextofkin va localdoctoc
                    if (patientBLL.Update(nhanpa).Equals("success"))
                    {
                        MessageBox.Show("Update patient successful");
                        gcPatients.DataSource = patientBLL.SelectDesc();
                    }
                    else
                    {
                        MessageBox.Show(patientBLL.Update(nhanpa));
                    }
                }
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string key = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gcAppointIdH).ToString();
            var mang = appointmentBLL.Search(key)[0];
            AddAppointPaIdH.Text = mang.PatientID;
            AddAppointStaffIdH.Text = mang.StaffID;
            appointmentid = mang.AppointmentID.ToString();
            ButtonEditAppoitH.Visible = true;
        }

        private void ButtonEditAppoitH_Click(object sender, EventArgs e)
        {
            AppointmentDTO nhanappoint = new AppointmentDTO();
            nhanappoint.Appointmentid = appointmentid;
            nhanappoint.Staffid = AddAppointStaffIdH.Text;
            nhanappoint.Patientid = AddAppointPaIdH.Text;
            nhanappoint.Date = AddAppointDateH.DateTime;
            nhanappoint.Time = AddAppointTimeH.Time.ToString();
            nhanappoint.Room = AddAppointRoomH.SelectedItem.ToString();
            nhanappoint.Description = AddAppointDescriptionH.Text;
            nhanappoint.Status = AddAppointStatusH.SelectedItem.ToString();
            if (appointmentBLL.Update(nhanappoint).Equals("success"))
            {
                MessageBox.Show("Update Apointment success");
                gcApointListInAddAppoint.DataSource = appointmentBLL.SelectDesc();
            }
            else
            {
                MessageBox.Show(appointmentBLL.Update(nhanappoint));
            }
        }
    }
}